package com.gdk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.utils.XVDateUtils;


/**
 * 导航controller
 * */

@Controller
public class NavigationController {
	
	@Autowired
    private NavigationService navigationService;
	/**
	 * 新增导航前查询路径
	 * */
	@RequestMapping("/preAddNavigation.do")
	public String preAddNavigation(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
	
		
		List<Map<String, Object>> allRoute = navigationService.selectAllRoute();
		request.setAttribute("allRoute", allRoute);
		return "jsp/navigation/navigation-add";
	}
	

	/**
	 * 新增导航
	 * */
	@ResponseBody
	@RequestMapping("/addNavigation.do")
	public int addNavigation(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("n_status", 1);
		map.put("n_ctime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("导航插入  :  "+map);
		
		int re_status = navigationService.addNavigation(map);
		System.out.println("结果 : "+re_status);
		return re_status;
	}
	
	/**
	 * 查询所有导航
	 * */
	@RequestMapping("/selectAllNavigation.do")
	public String selectAllNavigation(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		List<Map<String, Object>> allNavigation = null;
		
		allNavigation = navigationService.selectAllNavigation();

		int itemCount = allNavigation.size();
		
		for (Map<String, Object> map2 : allNavigation) {
			if(map2.get("n_ctime") != null) {
				int p = Integer.valueOf(map2.get("n_ctime").toString());
				if(p != 0) {
					map2.put("n_ctime", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allNavigation",allNavigation);
		return "jsp/navigation/navigation-list";
	}
	
	/**
	 *修改前查询导航详情
	 * */
	@RequestMapping("/preSelectNavigationDetails.do")
	public String preSelectNavigationDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> navigationDetails = navigationService.selectNavigationDetails(map);
		
		request.setAttribute("navigationDetails",navigationDetails);
		List<Map<String, Object>> allRoute = navigationService.selectAllRoute();
		request.setAttribute("allRoute", allRoute);
		return "jsp/navigation/navigation-edit";
	}
	
	/**
	 * 修改导航
	 * */
	@ResponseBody
	@RequestMapping("/updateNavigation.do")
	public int updateNavigation(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("导航 : "+map);
		int shop = navigationService.updateNavigationDetails(map);
		return shop;
	}


}